package com.hangim.pce.service.impl;

import com.hangim.pce.mapper.SysElementRuleMapper;
import com.hangim.pce.model.BaseDrugElement;
import com.hangim.pce.model.SysElementRule;
import com.hangim.pce.service.BaseDrugElementService;
import com.hangim.pce.service.SysElementRuleService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hangim.pce.common.service.impl.BaseEntityServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 成分规则表 服务实现类
 * </p>
 *
 * @author hongjiliang
 * @since 2018-10-19
 */
@Service
public class SysElementRuleServiceImpl extends BaseEntityServiceImpl<SysElementRuleMapper, SysElementRule> implements SysElementRuleService {

    @Autowired
    BaseDrugElementService baseDrugElementService;

    @Override
    public List<SysElementRule> getElementRulesByDrugId(Long drugId) {
        BaseDrugElement entity = new BaseDrugElement();
        entity.setDrugId(drugId);
        List<BaseDrugElement> drugElements = baseDrugElementService.selectAll(entity);
        List<SysElementRule> elementRules = new ArrayList<>();
        for (BaseDrugElement drugElement : drugElements) {
            elementRules.addAll(getElementRulesByElementId(drugElement.getElementId()));
        }
        return elementRules;
    }

    @Override
    public List<SysElementRule> getElementRulesByElementId(Long elementId) {
        return mapper.selectList(new EntityWrapper<SysElementRule>().eq("element_id", elementId));
    }
}
